<html>
<head><meta charset="utf-8"><title>debug_assert_hack for std? · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack.20for.20std.3F.html">debug_assert_hack for std?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="211860857"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack%20for%20std%3F/near/211860857" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack.20for.20std.3F.html#211860857">(Oct 01 2020 at 01:47)</a>:</h4>
<p>I was reminded in <a href="https://github.com/rust-lang/rust/issues/77220">#77220</a> that we still can't run debug-only asserts in std, and had a thought: we do have a mechanism that either panics or succeeds in core based on the user's codegen settings, overflow checking.  So we could hack a "useful only for UB checks" assert based on that.</p>
<p><code>debug_assert_hack!(c)</code> =&gt; <code>Sub::sub(0_u32, if c { 0 } else { 1 })</code>.</p>
<p>I can't decide whether this is a brilliant or horrible idea...</p>



<a name="211861047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack%20for%20std%3F/near/211861047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack.20for.20std.3F.html#211861047">(Oct 01 2020 at 01:50)</a>:</h4>
<p>we have <code>#[rustc_inherit_overflow_checks]</code> already, which is similar</p>



<a name="211861127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack%20for%20std%3F/near/211861127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack.20for.20std.3F.html#211861127">(Oct 01 2020 at 01:52)</a>:</h4>
<p>(Yes, that's why I put <code>Sub::sub</code> instead of <code>-</code>, because using the trait method gets that attribute without having to mention it.)</p>



<a name="211879722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack%20for%20std%3F/near/211879722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack.20for.20std.3F.html#211879722">(Oct 01 2020 at 08:08)</a>:</h4>
<p>You have to put <code>#[rustc_inherit_overflow_checks]</code> on <code>set_len</code> too. Also <code>#[rustc_inherit_overflow_checks]</code> is there to make <code>Sub::sub</code> behave the same way as <code>-</code> even when <code>Sub::sub</code> is not in the same crate as it's use.</p>



<a name="212008750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack%20for%20std%3F/near/212008750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/debug_assert_hack.20for.20std.3F.html#212008750">(Oct 02 2020 at 06:14)</a>:</h4>
<p>3 years ago that wasn't the case, see &lt;<a href="https://github.com/rust-lang/rust/pull/45754#issuecomment-341884683">https://github.com/rust-lang/rust/pull/45754#issuecomment-341884683</a>&gt;.  Of course it may have changed since then.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>